﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	//对视图中的<a>标记进行格式化,并收集表达式绑定信息.
	class XSNViewFormatHyperlinkWorker
	{	
		public static void formatHyperlink(View view)
		{
			XmlElement root = view.Doc.DocumentElement;
			string aFilter = "//a";
			string valueofFilter = NamespaceUtil.toFullName( view.XslPrefix, "attribute" ) + "[@name='href']/" + NamespaceUtil.toFullName( view.XslPrefix, "value-of" );
			
			XmlNodeList aElementList = DOMUtil.selectNodes(root, aFilter, 0, view.NsResolver);
			int hyperlinkIndex = 0;
			foreach(XmlElement aElement in aElementList)
			{
                //STATUS 0
                string className = aElement.GetAttribute("class");
                if (className != null && className.Equals("xdPictureButton"))
                {
                    continue;
                }

				aElement.SetAttribute( "target", "_blank" );

				//给所有的Href添加Id,以便能够在ValueChange时修改值.见MatterNo.xsn.
				XmlElement hrefValueOfElement = (XmlElement)DOMUtil.selectANode( aElement, valueofFilter, view.NsResolver );
				if( hrefValueOfElement != null )
				{
					//添加Id,以便能够在FormValueBuildHtmlFieldList中在ValueChange时动态修改href.
					aElement.SetAttribute( JetConstant.HTML_id, JetConstant.InfoJetHyperLink + "_" + view.Id + "_" + hyperlinkIndex );
				}
				hyperlinkIndex ++;

                //收集表达式绑定信息,测试件Forms2007\Hyperlink\Hyperlinks.xsn.
                if (className != null && className.Equals("xdDataBindingUI"))
				{
					string xslPrefix = view.XslPrefix;
					string allValueofFilter = ".//" + NamespaceUtil.toFullName(xslPrefix, "value-of");
					XmlNodeList valueofElementList = DOMUtil.selectNodes( aElement, allValueofFilter, 0, view.NsResolver );
					foreach( XmlElement valueofElement in valueofElementList )
					{
						string binding = valueofElement.GetAttribute( "select" );
						if( binding != null && binding.Length > 0 )
						{
							if( view.ExprBindingList == null )
							{
								view.ExprBindingList = new ArrayList();
							}

							view.ExprBindingList.Add( binding );
						}
					}
				}
			}
		}

		//CID 0235 //2009.03.03 //Begin
		public static void postFormatHyperlink( Manifest manifest )
		{
			if( manifest.HyperlinkList != null )
			{
				foreach (View view in manifest.ViewList)
				{	
					XmlElement root = view.Doc.DocumentElement;
					string aFilter = "//a";
					XmlNodeList aElementList = DOMUtil.selectNodes(root, aFilter, 0, view.NsResolver);
					foreach(XmlElement aElement in aElementList)
					{
						InfoJetSoft.Service.EscapeRule.Hyperlink.Execute( view, aElement );
					}
				}
			}	
		}
		//CID 0235 //2009.03.03 //End
	}
}